<?php
$cs=Yii::app()->getClientScript();
$cs->registerCoreScript('jquery');
$cs->registerScriptFile(SystemAsset::toUrl('jquery.validate.js'));
?>

<script type="text/javascript">
jQuery.validator.addMethod("exclude", function(value, element, param) {
    if(this.optional(element))
        return true;

    var s='';
    for(var i=0; i<param.length; ++i) {
        s+=param[i];
        if(i!=param.length-1) {
            s+='|';
        }
    }
    var pattern = new RegExp(s);
    return !pattern.test(value);
}, jQuery.format("不能包含非法字符"));

$(document).ready(function(){
    $("form").validate({
        rules: {
            '<?=CHtml::activeName($form,'nameOrEmail')?>': {
                required: true,
                rangelength: [2,12],
                exclude: ['@',' ']
            },
            '<?=CHtml::activeName($form,'password')?>': {
                required: true,
                rangelength: [6,20]
            }
        },
        messages: {
            '<?=CHtml::activeName($form,'nameOrEmail')?>': {
                required: "用户名不能为空",
                rangelength: jQuery.format("用户名长度为{0}到{1}个字"),
                exclude: "用户名不能包含@或空格"
            },
            '<?=CHtml::activeName($form,'password')?>': {
                required: "密码不能为空",
                rangelength: jQuery.format("密码长度为{0}到{1}个字")
            }
        },
        errorPlacement: function(error, element) {
            error.appendTo(element.next());
        },
        success: function(label) {
            label.html("&nbsp;").addClass("checked");
        },
        onkeyup: false
    });
});
</script>

<div>
  <h2 class="title">登录</h2>

  <?php echo CHtml::beginForm(); ?>
  <?php echo CHtml::errorSummary($form); ?>

  <p></p>
  
  <div>
    <label id="l<?=CHtml::activeId($form,'nameOrEmail')?>" for="<?=CHtml::activeId($form,'nameOrEmail')?>"><?=CHtml::encode($form->getAttributeLabel('nameOrEmail'))?></label>
  
  </div>
  <div>
    <?php echo CHtml::activeTextField($form,'nameOrEmail',array('class'=>'textfield')); ?>
    <span id='status'></span>
  </div>
  
  <p></p>
  
  <div>
    <label id="l<?=CHtml::activeId($form,'password')?>" for="<?=CHtml::activeId($form,'password')?>"><?=CHtml::encode($form->getAttributeLabel('password'))?></label>
  </div>
  <div>
    <?php echo CHtml::activePasswordField($form,'password',array('class'=>'textfield')); ?>
    <span id='status'></span>
  </div>
  
  <p></p>
  
  <div>
    <?php echo CHtml::activeCheckBox($form,'rememberMe'); ?>
    <?php echo CHtml::activeLabel($form,'rememberMe'); ?>
  </div>
  
  <p></p>

  <div class="submit">
    <?php echo CHtml::submitButton('登录'); ?>
  </div>

  <?php echo CHtml::endForm(); ?>
</div>